/**会员数据管理 */
import { defineStore } from "pinia";
import {
    getMemberList,
    getMemberTotal,
    editMember
} from '../api/memberApi'

import { ElMessage } from "element-plus";
// 注册store
const useMemberStore = defineStore("member", {
    state: () => {
        return {
            // 起始页码
            page: 1,
            // 单页展示数量
            size: 10,
            // 会员总数
            total: 0,
            // 储存数据的数组
            memberList: []
        }
    },
    getters: {

    },
    actions: {
        GET_MEMBERLIST(list) {
            this.memberList = list
        },
        CHANGE_PAGE(page) {
            this.page = page
        },
        CHANGE_SIZE(size) {
            this.size = size
        },
        CHANGE_TOTAL(total) {
            this.total = total
        },
        // 分页展示会员列表
        getMemberListAction() {
            getMemberList(this.page, this.size).then(response => {
                console.log('response', response)
                if (response.code === 200) {
                    console.log(response, "Member list")
                    this.GET_MEMBERLIST(response.list)
                }
            })
        },
        // 会员总数
        getMemberTotalAction() {
            getMemberTotal().then(response => {
                if (response.code === 200) {
                    // console.log(response, "Member total")
                    this.CHANGE_TOTAL(response.list[0].total)
                }
            })
        },
        // 编辑会员
        editMemberAction(memberForm) {
            editMember(memberForm).then(response => {
                if (response.code === 200) {
                    ElMessage({
                        type: 'success',
                        message: "会员编辑成功"
                    })
                    this.getMemberListAction()
                } else {
                    ElMessage({
                        type: "error",
                        message: response.msg
                    })
                }
            })
        },
    }
})
// 导出
export default useMemberStore